package com.example;

import java.util.Random;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
	int timeOut;
	Random r = new Random();
	int a, b, DiemSo, SoCauHoi;
	TextView lblTimeOut, txtA, txtB;

	EditText txtTimeOut;
	EditText txtKetQua;

	Button btnOK;

	public void pressOK(View v) {
		int KetQua = Integer.parseInt(txtKetQua.getText().toString());
		if (a + b == KetQua) {
			DiemSo++;
		}
		sinhCauHoi();
		txtKetQua.setText("");
	}

	void sinhCauHoi() {
		a = r.nextInt(50);
		b = r.nextInt(50);
		txtA.setText(a + "");
		txtB.setText(b + "");
		SoCauHoi++;
	}

	Handler warning = new Handler() {
		public void handleMessage(android.os.Message msg) {
		};
	};

	public void startGame(View v) {
		txtKetQua.setEnabled(true);
		btnOK.setEnabled(true);
		txtTimeOut = (EditText) findViewById(R.id.txtTimeOut);
		txtTimeOut.setEnabled(false);
		timeOut = Integer.parseInt(txtTimeOut.getText().toString());
		sinhCauHoi();
		Handler countDown = new Handler() {
			int t = timeOut;

			public void handleMessage(android.os.Message msg) {
				txtTimeOut.setText(t-- + "");
				
				if (t == -1) {
					txtKetQua.setEnabled(false);
					btnOK.setEnabled(false);
					lblTimeOut.setText("ket qua");
					txtTimeOut.setText(DiemSo + "/" + SoCauHoi + "/" + timeOut);
					return;
				}

				this.sendEmptyMessageDelayed(0, 1000);
			};

		};
		countDown.sendEmptyMessage(0);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txtKetQua = (EditText) findViewById(R.id.txtKetQua);
		btnOK = (Button) findViewById(R.id.btnOK);
		lblTimeOut = (TextView) findViewById(R.id.lblTimeOut);
		txtKetQua.setEnabled(false);
		btnOK.setEnabled(false);
		txtA = (TextView) findViewById(R.id.txtA);
		txtB = (TextView) findViewById(R.id.txtB);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}
